//
//  HWBuyAccountController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/9/19.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWBuyAccountController.h"
#import "HWZhanHaoTableViewCell.h"
#import "HWPayFangshiViewCell.h" // 购买方式Cell
#import "HWBuyAccountModel.h"
#import "HWSellTopView.h"
#import "HWBuySuccessGouMaiController.h" // 购买成功
#import "HWHWTradingPurchaseFailedController.h" // 购买失败
#import "HWWebZhibViewController.h" // 跳转支付网页
#import "HWWebViewGameViewController.h"
#import "HWLoginController.h" 
#import "UIBarButtonItem+Item.h"
@interface HWBuyAccountController ()<UITableViewDelegate,UITableViewDataSource,HWSellTopViewDelegate>
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) NSMutableArray *dataMuArr;
@property(strong,nonatomic) UIView    *lowerPartView;
@property(strong,nonatomic) UIView *footerView;
@property(strong,nonatomic) HWSellTopView *sellTopview;

@end

@implementation HWBuyAccountController

-(UITableView *)mainaTableVie {
    
    if (_mainaTableVie == nil
        ) {
        
        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, CF_NavHeight, kScreenW, kScreenH-CF_NavHeight-44-CF_TabbarSafeBottomMargin) style:UITableViewStyleGrouped];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
        
    }
    return   _mainaTableVie ;
}


- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    DLog(@"11111");
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 设置常用属性
    [self setCommonProperties];
    
    
    [self setupUI];
    
    [self addLowerPartView];
    
    [self addgtablviewHeadeView];
    
    // 
    [self tankuanView];
    
    
}

- (void)tankuanView {
    
    
    self.sellTopview = [[HWSellTopView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    self.sellTopview.delegate = self;
    
    [kKeyWindow addSubview: self.sellTopview];
    
    
    
    
}

// 放弃购买
- (void)HWSellTopViewGiveUpToBuy {
    
    
    [self.sellTopview  removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];

}

// 立即购买
- (void)HWSellTopViewBuy {
    
      [self.sellTopview  removeFromSuperview];
    
}

// 设置常用属性
- (void)setCommonProperties  {
    
    self.dataMuArr = [[NSMutableArray alloc]init];
    HWBuyAccountModel *modle1 = [[HWBuyAccountModel alloc]init];
    modle1.nameStr = @"余额支付";
    modle1.icoStr = @"icon_pt";
    modle1.seleico = NO;

    HWBuyAccountModel *modle2 = [[HWBuyAccountModel alloc]init];
    modle2.nameStr = @"平台币支付";
    modle2.icoStr = @"deal_icon_p";
    modle2.seleico = YES;
    HWBuyAccountModel *modle3 = [[HWBuyAccountModel alloc]init];
    
    modle3.nameStr = @"微信支付";
    modle3.icoStr = @"deal_icon_wxz";
    modle3.seleico = NO;
    HWBuyAccountModel *modle4 = [[HWBuyAccountModel alloc]init];
    
    modle4.nameStr = @"支付宝支付";
    modle4.icoStr = @"deal_icon_z";
    modle4.seleico = NO;
    
    [self.dataMuArr addObject:modle1];
    [self.dataMuArr addObject:modle2];
    [self.dataMuArr addObject:modle3];
    [self.dataMuArr addObject:modle4];
}



// 添加低部UIview
- (void)addLowerPartView {
    
    self.lowerPartView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenH-44-CF_TabbarSafeBottomMargin, kScreenW, 44+CF_TabbarSafeBottomMargin)];
    // self.lowerPartView.backgroundColor = [UIColor yellowColor];
    self.lowerPartView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.lowerPartView];
    
    UIView *xianView = [[UIView alloc]initWithFrame:CGRectMake(0, 44+CF_TabbarSafeBottomMargin-1, kScreenW, 1)];
    xianView.backgroundColor = RGB(215, 215, 215);
    [self.lowerPartView addSubview:xianView];
    
    
    UIButton *butt  = [UIButton setButtonWithFrame:CGRectMake(65, 7, kScreenW-65*2, 30) Title:@"确认支付" Image:nil Font:13 Color:RGB(255, 255, 255) Target:self Selector:@selector(qurenzhifuTap:)];
    butt.backgroundColor = RGB(38, 154, 255);
    butt.layer.cornerRadius = 15;
    butt.layer.masksToBounds = YES;
    [self.lowerPartView addSubview:butt];
    
}

// 确认支付
- (void)qurenzhifuTap:(UIButton *)sender {
    
    DLog(@"ij0sdhioi");
    
    
        if ([[HWLocallyStoredData getLoginUserID] isEqualToString:@""]) {
    
                HWLoginController *login = [[HWLoginController alloc]init];
                [self.navigationController pushViewController:login animated:YES];
    
               return;
         }
    
//    HWBuySuccessGouMaiController *buysuccess = [[HWBuySuccessGouMaiController alloc]init];
//    [self.navigationController pushViewController:buysuccess animated:YES];
    
    
//    HWHWTradingPurchaseFailedController  *tradingPurchaseFailed = [[HWHWTradingPurchaseFailedController alloc]init];
//    [self.navigationController pushViewController:tradingPurchaseFailed animated:YES];
    
    [self fasongwangluoqingqiu];
    
}


//发送网络请求
- (void)fasongwangluoqingqiu {
    
    
//    HWBuySuccessGouMaiController *suu = [[HWBuySuccessGouMaiController alloc]init];
//                 [self.navigationController pushViewController:suu animated:YES];
//
//    return;
//
    
    
     NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
      int t =  [WMUtil getRandomNumber:101 to:998];
      NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
      
    
      
      NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
      
      NSString *singMD5 = [WMUtil md5HexDigest:singStr];
    
    NSString *typestring = @"1";
    
    for (int i = 0 ; i < self.dataMuArr.count; i++) {

        HWBuyAccountModel *modle2 = self.dataMuArr[i];
        if (modle2.seleico) {
            typestring = [NSString stringWithFormat:@"%d",i];
        }
    
        
    }

        
     NSLog(@"打印 type ：%@",typestring);
       NSDictionary *parametes = @{

                                     @"ios" : @"1",
                                     @"ct" :  @"app",
                                     @"ac" : @"trade_buy",
                                     @"uid" :uidStr,
                                     @"t" :tStr,
                                     @"sign":singMD5,
                                     @"type" : typestring,
                                     @"goods_id" :self.idStr,
                                     @"money" : self.jiageStr,
                                    };



        [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {




             NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];

             if ([codeStr intValue] == 1) {
                 

                 
             if ([typestring isEqualToString:@"2"] ||[typestring isEqualToString:@"3"] ) {
                       NSString *urlstr =  response[@"data"][@"url"];
//                HWWebZhibViewController *zhifu = [[HWWebZhibViewController alloc]init];
//                zhifu.url = [NSURL URLWithString:urlstr];
//                [self.navigationController pushViewController:zhifu animated:YES];

                HWWebViewGameViewController *webview = [[HWWebViewGameViewController alloc]init];
                 webview.urlString = urlstr;
                  webview.gname = @"商品";
                 webview.hidesBottomBarWhenPushed = YES;
                 [self.navigationController pushViewController:webview animated:YES];

             } else  {
                
                HWBuySuccessGouMaiController *suu = [[HWBuySuccessGouMaiController alloc]init];
                [self.navigationController pushViewController:suu animated:YES];
                
                
                
            }
                 
                 




             } else {

                 
                  [MBProgressHUD showError:response[@"message"]];

             }



            [self.mainaTableVie reloadData];


         } failure:^(NSString *error) {
             [self.mainaTableVie.mj_header endRefreshing];
             [self.mainaTableVie.mj_footer endRefreshing];
         }];


    
    
}








- (void)setupUI {
    
    
    self.navigationItem.title = @"购买账号";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem  backItemWithimage:[UIImage imageNamed:@"fanhui"] highImage:[UIImage imageNamed:@"fanhui"] target:self action:@selector(back) title:@"   "];
    [self.view addSubview:self.mainaTableVie];
    
    
    
    
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
// 添加headeView
- (void)addgtablviewHeadeView {
    
    self.footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 246)];
    self.footerView.backgroundColor = [UIColor redColor];
    self.mainaTableVie.tableFooterView  =  self.footerView;
    
    
    UILabel *label  = [[UILabel alloc]initWithFrame:CGRectMake(13, 0, kScreenW-13*2, 36)];
    label.numberOfLines = 0;
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"买家须知" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang SC" size: 12],NSForegroundColorAttributeName: [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0]}];
    label.attributedText = string;
    [self.footerView addSubview:label];
    
    UIView *xianview = [[UIView alloc]initWithFrame:CGRectMake(0, 35, kScreenW, 1)];
    xianview.backgroundColor = RGB(232, 232, 242);
    [self.footerView addSubview:xianview];
    
    NSArray *arr = @[@"1.账号信息已通过我们官方审核，因时间变化造成排行榜，称号，显示道具到期等变化，不视为信息失实；",@"2.成功购买后角色直接转入您的账号，重新登录游戏即可进行查收并使用；",@"3.交易过程仅限角色转移，不存在平台账号交易或手机换绑行为，无需担心角色找回；",@"4.如因商品信息不实或其他因素问题，可联系客服核实协商解决。",@"购买后，不支持退货"];
    
    NSMutableArray  *arrH =  [[NSMutableArray alloc]init];
    for (int i = 0 ; i < arr.count; i++) {
        CGFloat h =  [self  calculateRowHeight:arr[i] fontSize:12];
        [arrH addObject:[NSString stringWithFormat:@"%f",h]];
        
        
    }
    
    for (int i = 0 ; i < arr.count; i++) {
        
        UILabel *label = [[UILabel alloc] init];
        label.numberOfLines = 0;
        label.font = [UIFont systemFontOfSize:12];
        label.text = arr[i];
        label.textColor = RGB(51, 51, 51);
        label.frame = CGRectMake(12, 0, kScreenW-12*2, 30);
        [self.footerView addSubview:label];
        
        if (i == 0) {
            CGFloat h0 = [arrH[0] floatValue];
            
            label.frame = CGRectMake(12, 36+10, kScreenW-12*2, h0);
            
            NSMutableAttributedString *atrtabl =  [WMUtil   attributedStringWithText:arr[i] withRangText:@"不视为信息失实" withColor:RGB(236, 11, 11) withFont:[UIFont systemFontOfSize:12]];
            
            
            label.attributedText = atrtabl;
            
            
            
            
        } else if (i == 1) {
            CGFloat h0 = [arrH[0] floatValue];
            CGFloat h1 = [arrH[1] floatValue];
            label.frame = CGRectMake(12,46+h0+i*10, kScreenW-12*2, h1);
            
        } else if (i == 2){
            
            CGFloat h0 = [arrH[0] floatValue];
            CGFloat h1 = [arrH[1] floatValue];
            CGFloat h2 = [arrH[2] floatValue];
            label.frame = CGRectMake(12,46+ h0+h1+i*10, kScreenW-12*2, h2);
          
            
            NSMutableAttributedString *atrtabl =  [WMUtil   attributedStringWithText:arr[i] withRangText:@"无需担心角色找回" withColor:RGB(236, 11, 11) withFont:[UIFont systemFontOfSize:12]];
            
            
            label.attributedText = atrtabl;
            
        }  else if (i == 3){
            
            CGFloat h0 = [arrH[0] floatValue];
            CGFloat h1 = [arrH[1] floatValue];
            CGFloat h2 = [arrH[2] floatValue];
            CGFloat h3 = [arrH[3] floatValue];
            label.frame = CGRectMake(12,46+ h0+h1+h2+i*10, kScreenW-12*2, h3);
            
           
            
        } else {
            
            CGFloat h0 = [arrH[0] floatValue];
            CGFloat h1 = [arrH[1] floatValue];
            CGFloat h2 = [arrH[2] floatValue];
            CGFloat h3 = [arrH[3] floatValue];
            CGFloat h4 = [arrH[4] floatValue];
            label.frame = CGRectMake(12, 46+h0+h1+h2+h3+i*10, kScreenW-12*2, h4);
            
            
            NSMutableAttributedString *atrtabl =  [WMUtil   attributedStringWithText:arr[i] withRangText:@"不支持退货" withColor:RGB(236, 11, 11) withFont:[UIFont systemFontOfSize:12]];
            label.attributedText = atrtabl;
            
            self.footerView.frame = CGRectMake(0, 90+CF_NavHeight, kScreenW, 50+h0+h1+h2+h3+h4+i*10);
            
            
        }
        self.footerView.backgroundColor = [UIColor whiteColor];
        
        
        
    }
    self.mainaTableVie.tableFooterView = self.footerView;
    
    
    
}

- (CGFloat)calculateRowHeight:(NSString *)string fontSize:(NSInteger)fontSize

{
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    
    //  NSDictionary *dic = @{NSFontAttributeName:[UIFont fontWithName:@"PingFang SC" size: 12]};
    
    CGRect rect = [string boundingRectWithSize:CGSizeMake(kScreenW-17*2, MAXFLOAT)/*计算高度要先指定宽度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                   
                   NSStringDrawingUsesFontLeading attributes:dic context:nil];
    
    return rect.size.height;
    
}



#pragma mark -------  UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    
    return  2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    if (section == 0) {
        return 1;
    }
    return  self.dataMuArr.count;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    if (indexPath.section == 0) {
        
        static NSString *cellID = @"cellID";
        HWZhanHaoTableViewCell  *cell =  [tableView dequeueReusableCellWithIdentifier:cellID];
        
        
        if (!cell) {
            cell =  [[NSBundle mainBundle]loadNibNamed:@"HWZhanHaoTableViewCell" owner:nil options:nil].lastObject;
            
        }
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        
        
        [cell.icoiamgeView sd_setImageWithURL:[NSURL URLWithString:self.urlstring] placeholderImage:nil];
        
        cell.mingchegLabel.text = self.name1str;
        cell.zhanghaomChenLabel.text = self.name2str;
        cell.leijichongzhiLabel.text = self.name3str;
        cell.jingeLabel.text = self.jiageStr;
        return  cell;
        
    } else {
        
         static NSString *cellID = @"cellID1";
         HWPayFangshiViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:cellID];
        
        if (!cell) {
            cell = [[HWPayFangshiViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            
        }
        cell.model = self.dataMuArr[indexPath.row];
        [cell.selecbutton addTarget:self action:@selector(selecbutTap:) forControlEvents:UIControlEventTouchUpInside];
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
         cell.backgroundColor = [UIColor whiteColor];
        return  cell;
    }
    
    
    
    
}


- (void)selecbutTap:(UIButton *)sender {
    
    NSLog(@"%@",sender.superview.superview);
    

    NSIndexPath *index =  [self.mainaTableVie indexPathForCell:(HWPayFangshiViewCell *)sender.superview.superview];
    
    for (int i =0 ; i < self.dataMuArr.count; i++) {
        
        HWBuyAccountModel *model = self.dataMuArr[i];
        model.seleico = NO;

    }

     HWBuyAccountModel *model = self.dataMuArr[index.row];
     model.seleico = YES;
     [self.mainaTableVie reloadData];
    



}




#pragma  mark -------------- UITableViewDelegate


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
          return  71;
    }else {
        
        return  50;
        
    }
   
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return  1;
    } else {
        
        return  36;
        
        
    }
    
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    

    return  6;
    
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW,6)];
    view.backgroundColor = RGB(245, 245, 245);
    return  view;
    
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    
    if (section == 0) {
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 1)];
        // view.backgroundColor = [UIColor orangeColor];
        view.backgroundColor = [UIColor whiteColor];
        return  view;
        
    } else {
        
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 36)];
        view.backgroundColor = [UIColor whiteColor];
        UILabel *xuanze = [[UILabel alloc]initWithFrame:CGRectMake(13, 0, 200, 36)];
        xuanze.text = @"请选择支付方式";
        xuanze.font = [UIFont systemFontOfSize:13];
        xuanze.textColor = RGB(51, 51, 51);
        [view addSubview:xuanze];
        UIView *backview = [[UIView alloc]initWithFrame:CGRectMake(0, 35, kScreenW, 1)];
        backview.backgroundColor = RGB(232, 232, 242);
        [view addSubview:backview];
        
        return view;
        

        
        
        
     
        
    }
    
    
    
    
    
}



@end
